package com.uenpay.im.ui.old.activity.dynamic;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.adapter.QiYunXinAdapter;
import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.message.type.MenuMsgType;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.weight.edittext.ExpressionUtil;
import com.qyx.android.weight.utils.QWeightTime;
import com.qyx.android.weight.utils.QWeightTime.TIMETYPE;
import com.qyx.android.weight.utils.QyxWeightTimeUtils;
import com.qyx.android.weight.view.MyGridView;
import com.uenpay.im.LinkDefs;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.ui.old.activity.chat.VideoPlayerActivity;
import com.uenpay.im.ui.old.activity.mycollect.CollectHandle;
import com.uenpay.im.adapter.TalkLongClickViewAdapter;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.TalkLongClickViewEntity;
import com.uenpay.im.view.PictureShowActivity;
import com.uenpay.im.weight.MyDialog;
import com.uenpay.im.weight.RoundImageView;

import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-3-10 下午1:32:04
 * @copyRight http://www.qiyunxin.com
 * @function 动态适配器
 */
public class DynamicAdapter extends QiYunXinAdapter {

    private DynamicAdapterViewHolder mDynamicAdapterViewHolder = null;
    private DynamicHandle mDynamicHandle = new DynamicHandle();
    private DynamicReplyHandler mReplyHandler = null;
    private EditText editText;
    private ArrayList<DynamicEntity> _dynamics;
    private Context mContext;
    private final IDynamicReplyListener mIDynamicReplyListener;

    public DynamicAdapter(EditText _editText, Context context,
                          ArrayList<DynamicEntity> dyList,
                          IDynamicReplyListener _IDynamicReplyListener) {
        editText = _editText;
        mDynamicAdapterViewHolder = new DynamicAdapterViewHolder();
        mReplyHandler = new DynamicReplyHandler(context);
        this._dynamics = dyList;
        this.mContext = context;
        this.mIDynamicReplyListener = _IDynamicReplyListener;
    }

    public void addData(ArrayList<DynamicEntity> dyArrayList) {
        this._dynamics.addAll(dyArrayList);
        notifyDataSetChanged();
    }

    public void setData(ArrayList<DynamicEntity> dyArrayList) {
        this._dynamics = dyArrayList;
        notifyDataSetChanged();

    }

    public void delData(int position) {
        this._dynamics.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        DynamicEntity dynamicEntity = _dynamics.get(position);
        int type = 0;
        if (dynamicEntity.files != null && dynamicEntity.files.size() < 1 && (dynamicEntity.videoFileMessage == null || dynamicEntity.videoConverFileMessage == null)) {
            type = 1;
        } else if (dynamicEntity.files != null
                && dynamicEntity.files.size() == 1) {
            type = 2;
        } else if (dynamicEntity.files != null
                && dynamicEntity.files.size() > 1) {
            type = 3;
        } else if (dynamicEntity.videoFileMessage != null && dynamicEntity.videoConverFileMessage != null) {
            type = 4;
        }
        switch (type) {
            case 1:
                return DynamicAdapterViewHolder.DYNAMIC_SIGNLE_TEXT;
            case 2:
                return DynamicAdapterViewHolder.DYNAMIC_ONE_IMAGE;
            case 3:
                return DynamicAdapterViewHolder.DYNAMIC_MULTI_IMAGE;
            case 4:
                return DynamicAdapterViewHolder.DYNAMIC_VIDEO;
            default:
                return DynamicAdapterViewHolder.DYNAMIC_SIGNLE_TEXT;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getCount() {
        return _dynamics.size();
    }

    @Override
    public DynamicEntity getItem(int position) {
        return _dynamics.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        DynamicAdapterViewHolder.DynamicBaseViewHolder baseViewHolder = mDynamicAdapterViewHolder.new DynamicBaseViewHolder();
        DynamicAdapterViewHolder.DynamicSignleTextViewHodler dynamicSignleTextViewHodler = null;
        DynamicAdapterViewHolder.DynamicOneImageViewHodler dynamicOneImageViewHodler = null;
        DynamicAdapterViewHolder.DynamicMultiImageViewHodler dynamicMultiImageViewHodler = null;
        DynamicAdapterViewHolder.DynamicVideoViewHodler dynamicVideoViewHodler = null;
        final DynamicEntity dy = _dynamics.get(position);
        int type = getItemViewType(position);
        if (view == null) {
            switch (type) {
                case DynamicAdapterViewHolder.DYNAMIC_SIGNLE_TEXT:
                    dynamicSignleTextViewHodler = mDynamicAdapterViewHolder.new DynamicSignleTextViewHodler();
                    view = LayoutInflater.from(mContext).inflate(
                            R.layout.item_dynamic_signle_text, parent, false);
                    dynamicSignleTextViewHodler.send_photo_iv = (RoundImageView) view
                            .findViewById(R.id.send_photo_iv);
                    dynamicSignleTextViewHodler.send_nick_tv = (TextView) view
                            .findViewById(R.id.send_nick_tv);
                    dynamicSignleTextViewHodler.send_head_tv = (TextView) view
                            .findViewById(R.id.send_head_tv);
                    dynamicSignleTextViewHodler.send_time_tv = (TextView) view
                            .findViewById(R.id.send_time_tv);
                    dynamicSignleTextViewHodler.del_tv = (TextView) view
                            .findViewById(R.id.del_tv);
                    dynamicSignleTextViewHodler.praise_ll = (LinearLayout) view
                            .findViewById(R.id.praise_ll);
                    dynamicSignleTextViewHodler.praise_tv = (TextView) view
                            .findViewById(R.id.praise_tv);
                    dynamicSignleTextViewHodler.mReplyLLayout = (LinearLayout) view
                            .findViewById(R.id.reply_content);
                    dynamicSignleTextViewHodler.bg_reply_content_layout = (LinearLayout) view
                            .findViewById(R.id.bg_reply_content_layout);
                    dynamicSignleTextViewHodler.view_like_reply = view
                            .findViewById(R.id.view_like_reply);
                    dynamicSignleTextViewHodler.reply_layout = (LinearLayout) view
                            .findViewById(R.id.reply_layout);
                    dynamicSignleTextViewHodler.praise_layout = (LinearLayout) view
                            .findViewById(R.id.praise_layout);
                    dynamicSignleTextViewHodler.praise_iv = (ImageView) view
                            .findViewById(R.id.praise_iv);
                    view.setTag(dynamicSignleTextViewHodler);
                    break;
                case DynamicAdapterViewHolder.DYNAMIC_ONE_IMAGE:
                    dynamicOneImageViewHodler = mDynamicAdapterViewHolder.new DynamicOneImageViewHodler();
                    view = LayoutInflater.from(mContext).inflate(
                            R.layout.item_dynamic_one_image, parent, false);

                    dynamicOneImageViewHodler.send_photo_iv = (RoundImageView) view
                            .findViewById(R.id.send_photo_iv);
                    dynamicOneImageViewHodler.send_nick_tv = (TextView) view
                            .findViewById(R.id.send_nick_tv);
                    dynamicOneImageViewHodler.mPicMaskIView = (MaskImageView) view
                            .findViewById(R.id.pic_one_iv);
                    dynamicOneImageViewHodler.send_head_tv = (TextView) view
                            .findViewById(R.id.send_head_tv);
                    dynamicOneImageViewHodler.send_time_tv = (TextView) view
                            .findViewById(R.id.send_time_tv);
                    dynamicOneImageViewHodler.del_tv = (TextView) view
                            .findViewById(R.id.del_tv);
                    dynamicOneImageViewHodler.praise_ll = (LinearLayout) view
                            .findViewById(R.id.praise_ll);
                    dynamicOneImageViewHodler.praise_tv = (TextView) view
                            .findViewById(R.id.praise_tv);
                    dynamicOneImageViewHodler.mReplyLLayout = (LinearLayout) view
                            .findViewById(R.id.reply_content);
                    dynamicOneImageViewHodler.bg_reply_content_layout = (LinearLayout) view
                            .findViewById(R.id.bg_reply_content_layout);
                    dynamicOneImageViewHodler.view_like_reply = view
                            .findViewById(R.id.view_like_reply);
                    dynamicOneImageViewHodler.reply_layout = (LinearLayout) view
                            .findViewById(R.id.reply_layout);
                    dynamicOneImageViewHodler.praise_layout = (LinearLayout) view
                            .findViewById(R.id.praise_layout);
                    dynamicOneImageViewHodler.praise_iv = (ImageView) view
                            .findViewById(R.id.praise_iv);
                    AddRecycleMaskImageView(dynamicOneImageViewHodler.mPicMaskIView);
                    view.setTag(dynamicOneImageViewHodler);
                    break;
                case DynamicAdapterViewHolder.DYNAMIC_MULTI_IMAGE:
                    dynamicMultiImageViewHodler = mDynamicAdapterViewHolder.new DynamicMultiImageViewHodler();
                    view = LayoutInflater.from(mContext).inflate(
                            R.layout.item_dynamic_multi_pic, parent, false);
                    dynamicMultiImageViewHodler.send_photo_iv = (RoundImageView) view
                            .findViewById(R.id.send_photo_iv);
                    dynamicMultiImageViewHodler.send_nick_tv = (TextView) view
                            .findViewById(R.id.send_nick_tv);
                    dynamicMultiImageViewHodler.send_head_tv = (TextView) view
                            .findViewById(R.id.send_head_tv);
                    dynamicMultiImageViewHodler.send_time_tv = (TextView) view
                            .findViewById(R.id.send_time_tv);
                    dynamicMultiImageViewHodler.picGridView = (MyGridView) view
                            .findViewById(R.id.pic_gd);
                    dynamicMultiImageViewHodler.del_tv = (TextView) view
                            .findViewById(R.id.del_tv);
                    dynamicMultiImageViewHodler.praise_ll = (LinearLayout) view
                            .findViewById(R.id.praise_ll);
                    dynamicMultiImageViewHodler.praise_tv = (TextView) view
                            .findViewById(R.id.praise_tv);
                    dynamicMultiImageViewHodler.mReplyLLayout = (LinearLayout) view
                            .findViewById(R.id.reply_content);
                    dynamicMultiImageViewHodler.bg_reply_content_layout = (LinearLayout) view
                            .findViewById(R.id.bg_reply_content_layout);
                    dynamicMultiImageViewHodler.view_like_reply = view
                            .findViewById(R.id.view_like_reply);
                    dynamicMultiImageViewHodler.reply_layout = (LinearLayout) view
                            .findViewById(R.id.reply_layout);
                    dynamicMultiImageViewHodler.praise_layout = (LinearLayout) view
                            .findViewById(R.id.praise_layout);
                    dynamicMultiImageViewHodler.praise_iv = (ImageView) view
                            .findViewById(R.id.praise_iv);
                    view.setTag(dynamicMultiImageViewHodler);

                    break;
                case DynamicAdapterViewHolder.DYNAMIC_VIDEO:
                    dynamicVideoViewHodler = mDynamicAdapterViewHolder.new DynamicVideoViewHodler();
                    view = LayoutInflater.from(mContext).inflate(
                            R.layout.item_dynamic_video, parent, false);
                    dynamicVideoViewHodler.send_photo_iv = (RoundImageView) view
                            .findViewById(R.id.send_photo_iv);
                    dynamicVideoViewHodler.send_nick_tv = (TextView) view
                            .findViewById(R.id.send_nick_tv);
                    dynamicVideoViewHodler.send_head_tv = (TextView) view
                            .findViewById(R.id.send_head_tv);
                    dynamicVideoViewHodler.send_time_tv = (TextView) view
                            .findViewById(R.id.send_time_tv);
                    dynamicVideoViewHodler.maskImageView = (MaskImageView) view
                            .findViewById(R.id.video_iv);
                    dynamicVideoViewHodler.del_tv = (TextView) view
                            .findViewById(R.id.del_tv);
                    dynamicVideoViewHodler.praise_ll = (LinearLayout) view
                            .findViewById(R.id.praise_ll);
                    dynamicVideoViewHodler.praise_tv = (TextView) view
                            .findViewById(R.id.praise_tv);
                    dynamicVideoViewHodler.mReplyLLayout = (LinearLayout) view
                            .findViewById(R.id.reply_content);
                    dynamicVideoViewHodler.bg_reply_content_layout = (LinearLayout) view
                            .findViewById(R.id.bg_reply_content_layout);
                    dynamicVideoViewHodler.view_like_reply = view
                            .findViewById(R.id.view_like_reply);
                    dynamicVideoViewHodler.reply_layout = (LinearLayout) view
                            .findViewById(R.id.reply_layout);
                    dynamicVideoViewHodler.praise_layout = (LinearLayout) view
                            .findViewById(R.id.praise_layout);
                    dynamicVideoViewHodler.praise_iv = (ImageView) view
                            .findViewById(R.id.praise_iv);
                    view.setTag(dynamicVideoViewHodler);
                    break;
            }

        } else {
            switch (type) {
                case DynamicAdapterViewHolder.DYNAMIC_SIGNLE_TEXT:
                    dynamicSignleTextViewHodler = (DynamicAdapterViewHolder.DynamicSignleTextViewHodler) view
                            .getTag();
                    break;
                case DynamicAdapterViewHolder.DYNAMIC_ONE_IMAGE:
                    dynamicOneImageViewHodler = (DynamicAdapterViewHolder.DynamicOneImageViewHodler) view
                            .getTag();
                    break;
                case DynamicAdapterViewHolder.DYNAMIC_MULTI_IMAGE:
                    dynamicMultiImageViewHodler = (DynamicAdapterViewHolder.DynamicMultiImageViewHodler) view
                            .getTag();
                    break;
                case DynamicAdapterViewHolder.DYNAMIC_VIDEO:
                    dynamicVideoViewHodler = (DynamicAdapterViewHolder.DynamicVideoViewHodler) view.getTag();
                    break;
            }
        }

        baseViewHolder = (DynamicAdapterViewHolder.DynamicBaseViewHolder) view
                .getTag();
        if (baseViewHolder != null) {
            String name = IMApplication.config.getFriendsRemarkName(dy.custid);
            if (TextUtils.isEmpty(name)) {
                name = dy.nickname.trim();
            }
            baseViewHolder.send_nick_tv.setText(name);
            if (TextUtils.isEmpty(dy.content)) {
                baseViewHolder.send_head_tv.setVisibility(View.GONE);
            } else {
                baseViewHolder.send_head_tv.setVisibility(View.VISIBLE);
                dy.content = Utils.StringFilter(dy.content);
                baseViewHolder.send_head_tv.setText(ExpressionUtil
                        .getExpressionString(mContext, dy.content));
                baseViewHolder.send_head_tv.setOnTouchListener(LinkDefs.mOnTouchListener);
                LinkDefs.extractUrl2Link(baseViewHolder.send_head_tv);
                baseViewHolder.send_head_tv
                        .setOnLongClickListener(new OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View v) {
                                Utils.hideSoftKeyboard(editText);
                                Message message = handler.obtainMessage();
                                message.what = 8;
                                message.obj = dy;
                                handler.sendMessage(message);
                                return true;
                            }
                        });
            }
            if ((dy.reply_list != null && dy.reply_list.size() > 0)
                    || (dy.like_list != null && dy.like_list.size() > 0)) {
                baseViewHolder.bg_reply_content_layout
                        .setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.bg_reply_content_layout.setVisibility(View.GONE);
            }

            if ((dy.reply_list != null && dy.reply_list.size() > 0)
                    && (dy.like_list != null && dy.like_list.size() > 0)) {
                baseViewHolder.view_like_reply.setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.view_like_reply.setVisibility(View.GONE);
            }

            if (dy.reply_list != null && dy.reply_list.size() > 0) {
                baseViewHolder.mReplyLLayout.setVisibility(View.VISIBLE);
                baseViewHolder.mReplyLLayout.removeAllViews();
            } else {
                baseViewHolder.mReplyLLayout.setVisibility(View.GONE);
            }

            baseViewHolder.send_photo_iv.SetUrl(APIConfiguration
                    .getAvatarUrlNormal(dy.custid, PacketType.PERSONAL));

            baseViewHolder.send_nick_tv
                    .setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            Utils.hideSoftKeyboard(editText);
                            Bundle bundle = new Bundle();
                            bundle.putString("cust_id", dy.custid);
                            bundle.putString("cust_name", dy.nickname);
                            ActivityHandle.startActivity((Activity) mContext,
                                    PersonalDynamicActivity.class, bundle, -1);
                        }
                    });
            baseViewHolder.send_photo_iv
                    .setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Utils.hideSoftKeyboard(editText);
                            Bundle bundle = new Bundle();
                            bundle.putString("cust_id", dy.custid);
                            bundle.putString("cust_name", dy.nickname);
                            ActivityHandle.startActivity((Activity) mContext,
                                    PersonalDynamicActivity.class, bundle, -1);
                        }
                    });
            baseViewHolder.del_tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    MyDialog.Builder alertDialog = new MyDialog.Builder(
                            mContext);
                    alertDialog.setTitle(R.string.dialog_title);
                    alertDialog.setMessage(mContext.getResources().getString(
                            R.string.delete_tv_dialog));
                    alertDialog.setPositiveButton(R.string.sure,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    dialog.dismiss();
                                    int doDynamicPos = (Integer) v.getTag();
                                    delDynamic(doDynamicPos);
                                }
                            });

                    alertDialog
                            .setNegativeButton(
                                    R.string.cancel,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            dialog.dismiss();
                                        }
                                    });
                    alertDialog.create().show();

                }
            });

            if (dy._praise == 1) {
                baseViewHolder.praise_iv
                        .setImageResource(R.drawable.icon_praise_selected);
            } else {
                baseViewHolder.praise_iv
                        .setImageResource(R.drawable.icon_praise_normal);
            }

            baseViewHolder.praise_layout
                    .setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            boolean isPraise = false;
                            if (dy.like_list != null) {
                                for (Praise p : dy.like_list) {
                                    if (p.custid.equals(IMApplication
                                            .getCustId())) {
                                        isPraise = true;
                                    }
                                }
                            }
                            if (!isPraise) {
                                praiseDynamic(dy, 1);
                            } else {
                                praiseDynamic(dy, 0);
                            }

                        }
                    });
        }
        baseViewHolder.reply_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIDynamicReplyListener.onDynamicReply(
                        Integer.valueOf(dy.dynamicid), 0, position);
            }
        });
        QWeightTime mQWeightTime = QyxWeightTimeUtils.compareDateByMillis(
                mContext, dy.createtime, QyxWeightTimeUtils._standardFormat);
        String str = "";
        if (mQWeightTime.type == TIMETYPE.DAY) {
            str = MessageFormat.format(
                    mContext.getResources().getString(R.string.many_days_ago),
                    mQWeightTime.result);
        } else if (mQWeightTime.type == TIMETYPE.HOUR) {
            str = MessageFormat.format(
                    mContext.getResources().getString(R.string.many_hour_ago),
                    mQWeightTime.result);
        } else if (mQWeightTime.type == TIMETYPE.MINUTE) {
            str = MessageFormat.format(
                    mContext.getResources()
                            .getString(R.string.many_minutes_ago),
                    mQWeightTime.result);
        } else {
            str = mContext.getResources().getString(R.string.just);
        }
        if (TextUtils.isEmpty(str)) {
            baseViewHolder.send_time_tv.setText(dy.createtime);
        } else {
            baseViewHolder.send_time_tv.setText(str);
        }
        if (dy.custid.equals(IMApplication.getCustId())) {
            baseViewHolder.del_tv.setVisibility(View.VISIBLE);
        } else {
            baseViewHolder.del_tv.setVisibility(View.GONE);
        }
        baseViewHolder.del_tv.setTag(position);

        mReplyHandler.setPraiseView(dy.like_list, baseViewHolder.praise_ll,
                baseViewHolder.praise_tv);

        if (dy.reply_list != null) {
            for (int i = 0; i < dy.reply_list.size(); i++) {
                View replyText = mReplyHandler.getReplyTextView(position,
                        editText, dy.reply_list.get(i), i,
                        baseViewHolder.mReplyLLayout, new DynamicReplyHandler.ITextLinkListener() {

                            @Override
                            public void onTextLink(int dyPos, int status,
                                                   String custId, String pCustId,
                                                   int deleteReplyIndx,
                                                   LinearLayout linearLayout) {
                                if (status == HttpInvokeResultCode.RESULT_OK) {
                                    delMyReply(custId, position,
                                            deleteReplyIndx);
                                } else {
                                    mIDynamicReplyListener.onDynamicReply(
                                            Integer.valueOf(dy.dynamicid),
                                            Integer.valueOf(pCustId), position);
                                    // replyLayout = linearLayout;
                                    // is_restore = true;
                                    // source_cust_id = pCustId;
                                    // dynamic = adapter.getItem(position);
                                    // doDynamicPos = dyPos;
                                    // showPop();
                                }
                            }
                        });
                baseViewHolder.mReplyLLayout.addView(replyText);

            }
        }
        baseViewHolder.position = position;
        switch (type) {
            case DynamicAdapterViewHolder.DYNAMIC_SIGNLE_TEXT:

                break;
            case DynamicAdapterViewHolder.DYNAMIC_ONE_IMAGE:
                dynamicOneImageViewHodler.mPicMaskIView.SetRecyclableUrl(
                        APIConfiguration.getFileDownloadPath(
                                dy.files.get(0).fileid, dy.files.get(0).filehash),
                        true);
                dynamicOneImageViewHodler.mPicMaskIView.setAdjustViewBounds(true);
                // setImageSize(dynamicOneImageViewHodler.mPicMaskIView,
                // dy.files.get(0));
                // Bitmap bt = BitmapCache.getInstance().GetBitmap(
                // APIConfiguration.getFileDownloadPath(
                // dy.files.get(0).fileid,
                // dy.files.get(0).filehash),"0_0");
                // if(bt != null){
                // int[] w_h =
                // ImageUtils.getImageWidthAndHeightToDynamic(DynamicActivity.this,
                // bt.getWidth(),
                // bt.getHeight());
                // ViewGroup.LayoutParams ivLinearParams =
                // dynamicOneImageViewHodler.mPicMaskIView
                // .getLayoutParams();
                // ivLinearParams.height = w_h[1];
                // ivLinearParams.width = w_h[0];
                // dynamicOneImageViewHodler.mPicMaskIView
                // .setLayoutParams(ivLinearParams);
                // }
                dynamicOneImageViewHodler.mPicMaskIView
                        .setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                Utils.hideSoftKeyboard(editText);
                                Intent show = new Intent(mContext,
                                        PictureShowActivity.class);
                                show.putExtra("content", APIConfiguration
                                        .getFileDownloadPath(
                                                dy.files.get(0).fileid,
                                                dy.files.get(0).filehash));
                                show.putExtra("isClearCache", false);
                                ArrayList<String> showpics = new ArrayList<String>();
                                showpics.add(APIConfiguration.getFileDownloadPath(
                                        dy.files.get(0).fileid,
                                        dy.files.get(0).filehash));
                                show.putStringArrayListExtra("imgList", showpics);
                                mContext.startActivity(show);
                            }
                        });

                dynamicOneImageViewHodler.mPicMaskIView
                        .setOnLongClickListener(new OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View v) {
                                Utils.hideSoftKeyboard(editText);

                                return true;
                            }
                        });
                break;
            case DynamicAdapterViewHolder.DYNAMIC_MULTI_IMAGE:
                DynamicPicAdapter adapter = new DynamicPicAdapter(mContext,
                        dy.files);
                dynamicMultiImageViewHodler.picGridView.setAdapter(adapter);
                break;
            case DynamicAdapterViewHolder.DYNAMIC_VIDEO:
                // TODO: 2017/3/1
                dynamicVideoViewHodler.maskImageView.SetUrl(APIConfiguration.getFileDownloadPath(dy.videoConverFileMessage.fileid, dy.videoConverFileMessage.filehash));
                dynamicVideoViewHodler.maskImageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dy.videoFileMessage != null) {
                            Intent intent = new Intent(mContext, VideoPlayerActivity.class);
                            intent.putExtra("path", APIConfiguration.getFileDownloadPath(dy.videoFileMessage.fileid, dy.videoFileMessage.filehash));
                            mContext.startActivity(intent);
                        }
                    }
                });
                break;
        }
        return view;
    }

    private Handler handler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 7) {
                ArrayList<FileMessage> imgs = (ArrayList<FileMessage>) msg.obj;
                int handle_position = msg.arg2;
                ArrayList<String> allImgs = new ArrayList<String>();
                for (FileMessage fileMessage : imgs) {
                    String url = APIConfiguration.getFileDownloadPath(
                            fileMessage.fileid, fileMessage.filehash);
                    allImgs.add(url);
                }

                Intent show = new Intent(mContext, PictureShowActivity.class);
                show.putExtra(
                        "content",
                        APIConfiguration.getFileDownloadPath(
                                imgs.get(handle_position).fileid,
                                imgs.get(handle_position).filehash));
                show.putStringArrayListExtra("imgList", allImgs);
                mContext.startActivity(show);
            } else if (msg.what == 8) {
                showMoreDialog((DynamicEntity) msg.obj);
            }

            return false;
        }
    });

    /**
     * @param rid
     * @param dynamicPosition
     * @param replyPosition
     */
    public void delMyReply(String rid, final int dynamicPosition,
                           final int replyPosition) {
        mDynamicHandle.deleteReply(rid, new DynamicHandle.IDynamicResult() {

            @Override
            public void onDynamicResult(int status, String msg) {
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    ArrayList<Reply> reply_list = _dynamics
                            .get(dynamicPosition).reply_list;
                    reply_list.remove(replyPosition);
                    notifyDataSetChanged();
                }
            }
        });
    }

    /**
     * @param dynamicPosition
     */
    private void delDynamic(final int dynamicPosition) {
        mDynamicHandle.deleteDynamic(
                Integer.parseInt(_dynamics.get(dynamicPosition).dynamicid),
                new DynamicHandle.IDynamicResult() {

                    @Override
                    public void onDynamicResult(int status, String msg) {
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            _dynamics.remove(dynamicPosition);
                            notifyDataSetChanged();
                        } else if (!TextUtils.isEmpty(msg)) {
                            IMApplication.showToast(msg);
                        }
                    }
                });

    }

    /**
     * @param dynamic
     * @param op
     */
    public void praiseDynamic(final DynamicEntity dynamic, final int op) {
        mDynamicHandle.praiseDynamic(Integer.parseInt(dynamic.dynamicid), op,
                new DynamicHandle.IDynamicResult() {

                    @Override
                    public void onDynamicResult(int status, String msg) {
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (op == 1) {
                                Praise p = new Praise();
                                p.custid = IMApplication.getCustId();
                                p.nickname = IMApplication.config
                                        .getUserName();
                                if (dynamic.like_list == null) {
                                    dynamic.like_list = new ArrayList<Praise>();
                                    dynamic.like_list.add(p);
                                } else {
                                    dynamic.like_list.add(0, p);
                                }
                                dynamic._praise = 1;
                            } else {
                                // cancel praise
                                for (int i = 0, size = dynamic.like_list.size(); i < size; i++) {
                                    if (dynamic.like_list.get(i).custid
                                            .equals(IMApplication.getCustId())) {
                                        dynamic.like_list.remove(i);
                                        break;
                                    }
                                }
                                dynamic._praise = 0;
                            }
                            for (int i = 0, size = _dynamics.size(); i < size; i++) {
                                if (dynamic.dynamicid.equals(_dynamics.get(i).dynamicid)) {
                                    _dynamics.get(i)._praise = op;
                                    break;
                                }
                            }
                            notifyDataSetChanged();
                            // mReplyHandler.setPraiseView(dynamic.like_list,
                            // praiseLin, praiseTv);
                        } else {
                            IMApplication.showToast(R.string.zan_failed);
                        }
                    }
                });

    }

    /**
     * @param mReply
     * @param dynamicId
     */
    public void updateReply(Reply mReply, int dynamicId) {
        for (int i = 0, size = _dynamics.size(); i < size; i++) {
            if (Integer.valueOf(_dynamics.get(i).dynamicid) == dynamicId) {
                ArrayList<Reply> add_reply_list = _dynamics.get(i).reply_list;
                add_reply_list.add(mReply);
                notifyDataSetChanged();
                break;
            }
        }
    }

    /**
     * @author SL
     * @copyRight http://www.qiyunxin.com
     */
    public interface IDynamicReplyListener {
        void onDynamicReply(int dynamicId, int source_cust_id,
                            int position);
    }

    /**
     * 收藏对话框
     *
     * @param dynamic
     */
    private void showMoreDialog(final DynamicEntity dynamic) {
        final MyDialog.Builder dlg = new MyDialog.Builder(mContext);

        ArrayList<TalkLongClickViewEntity> arrayList = new ArrayList<>();
        TalkLongClickViewEntity copy = new TalkLongClickViewEntity(
                MenuMsgType.COPY, R.string.copy);
        TalkLongClickViewEntity collect = new TalkLongClickViewEntity(
                MenuMsgType.COLLECT, R.string.collect);
        arrayList.add(copy);
        arrayList.add(collect);

        final ListView menus = new ListView(mContext);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        menus.setDivider(new ColorDrawable(mContext.getResources().getColor(
                R.color.layout_division_view)));
        menus.setDividerHeight(1);
        menus.setLayoutParams(params);
        TalkLongClickViewAdapter adapter = null;
        adapter = new TalkLongClickViewAdapter(mContext, arrayList);

        if (adapter != null) {
            menus.setAdapter(adapter);
        }
        dlg.setListView(menus);
        dlg.setMessage("");
        dlg.create().show();

        menus.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                TalkLongClickViewEntity mTalkLongClickViewEntity = (TalkLongClickViewEntity) parent
                        .getAdapter().getItem(position);
                if (mTalkLongClickViewEntity != null) {
                    if (mTalkLongClickViewEntity.type == MenuMsgType.COPY) {
                        String copy_str = dynamic.content;
                        if (!TextUtils.isEmpty(copy_str)) {
                            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) mContext
                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                clipboard.setText(copy_str);
                            } else {
                                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) mContext
                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                android.content.ClipData clip = android.content.ClipData
                                        .newPlainText("Copied Text", copy_str);
                                clipboard.setPrimaryClip(clip);
                            }
                            IMApplication
                                    .showToast(R.string.text_copied_to_the_clipboard);
                        }
                    }else if (mTalkLongClickViewEntity.type == MenuMsgType.COLLECT) {
                        CollectHandle mCollectHandle = new CollectHandle();
                        mCollectHandle.addCollect(103, dynamic.custid + "",
                                dynamic.nickname, dynamic.nickname, dynamic.content, "",
                                "", 0, null);
                    }
                }
                dlg.getDialog().dismiss();
            }
        });

    }

}
